/*
 * $Id: RevisionsTableCreator.java 488 2008-08-20 16:59:47Z rcleveng $
 * 
 * Copyright (c) 2001,2002 Boris Pruessmann and others. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v0.5 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.ui.internal.views;

import net.sourceforge.perforce.ui.internal.Policy;

import org.eclipse.jface.viewers.ColumnLayoutData;
import org.eclipse.jface.viewers.ColumnPixelData;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.IMemento;

/**
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pr??mann</a>
 */
public class RevisionsTableCreator {
  static final int COL_REVISION = 0;
  static final int COL_FILENAME = 1;
  static final int COL_CHANGELIST = 2;
  static final int COL_DATE = 3;
  static final int COL_USER = 4;
  static final int COL_ACTION = 5;
  static final int COL_DESCRIPTION = 6;

  @SuppressWarnings("unused")
  private static final String TAG_FOLLOW_BRANCHES = "follow-branches"; //$NON-NLS-1$
  private static final String TAG_COLUMN = "column"; //$NON-NLS-1$
  private static final String TAG_NUMBER = "number"; //$NON-NLS-1$
  private static final String TAG_WIDTH = "width"; //$NON-NLS-1$

  private static String[] columnHeaders = {Policy.bind("HistoryView.revision"), //$NON-NLS-1$
      Policy.bind("HistoryView.filename"), //$NON-NLS-1$
      Policy.bind("HistoryView.changelist"), //$NON-NLS-1$
      Policy.bind("HistoryView.date"), //$NON-NLS-1$
      Policy.bind("HistoryView.user"), //$NON-NLS-1$
      Policy.bind("HistoryView.action"), //$NON-NLS-1$
      Policy.bind("HistoryView.description") //$NON-NLS-1$
      };

  private static ColumnLayoutData[] columnLayouts =
      {new ColumnWeightData(10, true), new ColumnWeightData(10, true),
          new ColumnWeightData(10, true), new ColumnWeightData(10, true),
          new ColumnWeightData(10, true), new ColumnWeightData(10, true),
          new ColumnWeightData(40, true)};

  public static Table createTable(Composite parent, IMemento memento) {
    Table table = new Table(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI | SWT.FULL_SELECTION);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    table.setLayoutData(new GridData(GridData.FILL_BOTH));

    TableLayout layout = new TableLayout();
    table.setLayout(layout);

    createColumns(table, layout, memento);

    return table;
  }

  /** Create the table's columns. */
  private static void createColumns(Table table, TableLayout layout, IMemento memento) {
    if (memento != null) {
      IMemento children[] = memento.getChildren(TAG_COLUMN);
      if (children != null) {
        for (int i = 0; i < children.length; i++) {
          Integer val = children[i].getInteger(TAG_NUMBER);
          if (val != null) {
            int index = val.intValue();
            val = children[i].getInteger(TAG_WIDTH);
            if (val != null) {
              columnLayouts[index] = new ColumnPixelData(val.intValue(), true);
            }
          }
        }
      }
    }

    for (int i = 0, lentgh = columnHeaders.length; i < lentgh; i++) {
      TableColumn column = new TableColumn(table, SWT.NONE);
      column.setResizable(true);
      column.setText(columnHeaders[i]);
      layout.addColumnData(columnLayouts[i]);
    }
  }
}
